package GUI.Productos;

import java.rmi.RemoteException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import dbExceptions.DataBaseErrorException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.article.interfaces.ArticleProxyMgt;
import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import article.entities.ArticleVO;
import article.entities.IngredientVO;
import article.exceptions.BadArticleException;
import article.exceptions.MissingArticleException;
import article.exceptions.NotHereException;

public class EliminarProducto extends Dialog {

	protected Object result;
	protected Shell shell;
	ArticleVO articulo;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public EliminarProducto(Shell parent, int style, ArticleVO art) {
		super(parent, style);
		setText("Informacion del Articulo a Modificar");
		articulo=art;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(450, 373);
		shell.setText(getText());
		shell.setLayout(null);
		
		Label lblNombre = new Label(shell, SWT.NONE);
		lblNombre.setBounds(43, 38, 59, 14);
		lblNombre.setText("Nombre");
		
		Label lblPrecio = new Label(shell, SWT.NONE);
		lblPrecio.setBounds(43, 100, 59, 14);
		lblPrecio.setText("Precio");
		
		Label lblCategoria = new Label(shell, SWT.NONE);
		lblCategoria.setBounds(43, 159, 59, 14);
		lblCategoria.setText("Categoria");
		
		Label lblDescripcion = new Label(shell, SWT.NONE);
		lblDescripcion.setBounds(43, 274, 67, 14);
		lblDescripcion.setText("Descripcion");
		
		Label lblIngredientes = new Label(shell, SWT.NONE);
		lblIngredientes.setBounds(43, 215, 81, 14);
		lblIngredientes.setText("Ingredientes");
		
		Label label = new Label(shell, SWT.SEPARATOR | SWT.VERTICAL);
		label.setBounds(182, 40, 17, 248);
		
		Label label_1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_1.setBounds(43, 49, 356, 13);
		
		Label label_2 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_2.setBounds(43, 111, 356, 13);
		
		Label label_3 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_3.setBounds(43, 173, 356, 13);
		
		Label label_4 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_4.setBounds(43, 235, 356, 13);
		
		Label nombre = new Label(shell, SWT.NONE);
		nombre.setBounds(205, 38, 81, 14);
		nombre.setText(articulo.getName());
		
		Label precio = new Label(shell, SWT.NONE);
		precio.setBounds(205, 90, 59, 14);
		precio.setText(String.valueOf(articulo.getPrice()));
		
		Label categoria = new Label(shell, SWT.NONE);
		categoria.setBounds(205, 159, 81, 14);
		categoria.setText(articulo.getCat().getCat());
		
		Label ingredientes = new Label(shell, SWT.NONE);
		ingredientes.setBounds(205, 215, 194, 14);
		ingredientes.setText("New Label");
		IngredientVO [] ings = articulo.getIngr();
		String m="";
		for(int i = 0; i<ings.length;i++){
		m= m.concat(" " + ings[i].getIngr());
		}
		
		ingredientes.setText(m);
		
		Label descripcion = new Label(shell, SWT.NONE);
		descripcion.setBounds(205, 274, 171, 14);
		descripcion.setText(articulo.getDesc());
		
		Button btnEliminar = new Button(shell, SWT.NONE);
		btnEliminar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
				try {
					uso.deleteArticle(articulo);
					shell.close();
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (NotHereException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (MissingArticleException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (BadArticleException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (DataBaseErrorException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con la Base de Datos");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				}
				
			}
		});
		btnEliminar.setBounds(148, 313, 94, 28);
		btnEliminar.setText("Eliminar");
		
		Button btnCancelar = new Button(shell, SWT.NONE);
		btnCancelar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
			}
		});
		btnCancelar.setBounds(324, 313, 94, 28);
		btnCancelar.setText("Cancelar");

	}
}
